package com.study.thread.poll;


import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author mjy
 * @date 2021/12/02
 * @description
 */
public class CreateTestPolll {

    //参数初始化
    private static final int CPU_COUNT=Runtime.getRuntime().availableProcessors();

    //核心线程数量大小
    private static final int corePoolSize=Math.max(2,Math.min(CPU_COUNT-1,4));

    //线程池最大容纳线程数
    private static final int maximumPoolSize = CPU_COUNT * 2 + 1;

    //线程闲置后的存活时长
    private static final int keepAliveTime = 30;

    //任务过多后，存储任务一个阻塞队列
    BlockingQueue<Runnable> workQueue = new SynchronousQueue<>();

    //线程池的创建工厂
    ThreadFactory threadFactory=new ThreadFactory(){

        private final AtomicInteger mCount=new AtomicInteger();
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r,"AdvacnedAsyncTask #"+mCount.getAndIncrement());
        }
    };

    //线程池满载后采取的任务拒绝策略
    RejectedExecutionHandler rejectHandler= new ThreadPoolExecutor.DiscardOldestPolicy();
    //线程池对象创建线程
    //线程池对象，创建线程
    ThreadPoolExecutor mExecute = new ThreadPoolExecutor(
            corePoolSize,
            maximumPoolSize,
            keepAliveTime,
            TimeUnit.SECONDS,
            workQueue,
            threadFactory,
            rejectHandler
    );



}
